package probe.logic;

import java.io.IOException;
import java.util.ArrayList;

public class breakdown {

	private static final String COMMAND_ADD = "add";
	private static final String COMMAND_DISPLAY = "display";
	private static final String COMMAND_DELETE = "delete";
	private static final String COMMAND_UNDO = "undo";
	private static final String COMMAND_UPDATE = "update";
	private static final String COMMAND_EXIT = "exit";

	private static final String KEYWORD_AT = " -at ";
	private static final String KEYWORD_BY = " -by ";
	private static final String KEYWORD_FROM = " -from ";
	private static final String KEYWORD_TO = " -to ";
	private static final String KEYWORD_AS = " -as ";

	enum COMMAND_TYPE {
		ADD, DISPLAY, DELETE, UNDO, EXIT, UPDATE, INVALID
	}

	public static COMMAND_TYPE findCommandType(String command) {
		if (command.equalsIgnoreCase(COMMAND_ADD)) {
			return COMMAND_TYPE.ADD;
		} else if (command.equalsIgnoreCase(COMMAND_DISPLAY)) {
			return COMMAND_TYPE.DISPLAY;
		} else if (command.equalsIgnoreCase(COMMAND_DELETE)) {
			return COMMAND_TYPE.DELETE;
		} else if (command.equalsIgnoreCase(COMMAND_UNDO)) {
			return COMMAND_TYPE.UNDO;
		} else if (command.equalsIgnoreCase(COMMAND_EXIT)) {
			return COMMAND_TYPE.EXIT;
		} else if (command.equalsIgnoreCase(COMMAND_UPDATE)) {
			return COMMAND_TYPE.UPDATE;
		} else {
			return COMMAND_TYPE.INVALID;
		}
	}

	public static int getIndexOfFirstSpace(String command) {
		return command.indexOf(" ");
	}

	public static String extractFirstWord(String command) {
		int indexOfFirstSpace = getIndexOfFirstSpace(command);
		if (indexOfFirstSpace == -1) {
			return command;
		} else {
			return command.substring(0, indexOfFirstSpace);
		}
	}

	public static ArrayList<Task> processCommand(String command)
			throws IOException {
		String firstWord = extractFirstWord(command);
		COMMAND_TYPE commandType = findCommandType(firstWord);

		ArrayList<Task> listTask = new ArrayList<Task>();
		listTask = XML.getListOfTasksFromXml();

		String[] commandComponents = breakDownCommandIntoComponents(command);
		switch (commandType) {
		case ADD:
			Add add = new Add();
			listTask = add.process(listTask, commandComponents);
			break;
		case DISPLAY:
			Search search = new Search();
			search.process(listTask, commandComponents[0]);
			break;
		case DELETE:
			Delete delete = new Delete();
			listTask = delete.process(listTask, commandComponents[0]);
			break;
		case UNDO:
			// to be implemented
			break;
		case EXIT:
			// to be implemented
			break;
		case UPDATE:
			// to be implemented
			break;
		default:
			// to be implemented
			break;
		}

		XML.exportListOfTasksToXml(listTask);
		return listTask;

	}

	/*
	 * ///example
	 * 
	 * probe.Gui.printOutput(probe.Gui.COMMAND,"%s :with %d... arguments\n"
	 * ,"either use printf syntax",123);
	 * probe.Gui.printOutput(probe.Gui.SUB_COMMAND
	 * ,"or use normal text: 1 argument\n");
	 * 
	 * //make sure to have a new line to break style
	 * probe.Gui.printOutput(probe.Gui.COMMAND,listTask.toString()+"\n");
	 * probe.Gui.printOutput(probe.Gui.NORMAL,
	 * "public static void printOutput(Style style,String printfIdentifier, Object ... args)\nsee breakdown example, this helps to encapsulate yr code to remain in your code\n"
	 * );
	 */

	/*
	 * This method return a String array of different components of the command
	 * Each components will contain separate distinguishable parts of the
	 * command e.g. "add go to school -at 20/11/2012", it will return
	 * String[]{"go to school", "-at 20/11/2012"}
	 * "update have lunch at home -from 1200 -to 1330", it will return
	 * String[]{"have lunch at home","-from 1200","-to 1330"} for
	 * "update submit report -as done", it will return
	 * String[]{"submit report","-as done"}
	 */

	public static String[] breakDownCommandIntoComponents(String command) {
		int indexOfFirstSpace = getIndexOfFirstSpace(command);

		String[] components;

		if (indexOfFirstSpace == -1) {
			return new String[0];
		} else {
			int lastIndexOfAt = command.lastIndexOf(KEYWORD_AT);
			int lastIndexOfBy = command.lastIndexOf(KEYWORD_BY);
			int lastIndexOfFrom = command.lastIndexOf(KEYWORD_FROM);
			int lastIndexOfTo = command.lastIndexOf(KEYWORD_TO);
			int lastIndexOfAs = command.lastIndexOf(KEYWORD_AS);

			if (lastIndexOfAt > lastIndexOfBy && lastIndexOfAt > lastIndexOfTo) {

				// deadline task, format: ... -at ...

				String taskName = "", atTime = "";

				for (int i = indexOfFirstSpace + 1; i < lastIndexOfAt; i++)
					taskName += command.charAt(i);
				for (int i = lastIndexOfAt + KEYWORD_AT.length(); i < command
						.length(); i++)
					atTime += command.charAt(i);

				components = new String[] { taskName,
						KEYWORD_AT.substring(1) + atTime };

			} else if (lastIndexOfBy > lastIndexOfAt
					&& lastIndexOfBy > lastIndexOfTo) {

				// deadline task, format: ... -by ...

				String taskName = "", byTime = "";

				for (int i = indexOfFirstSpace + 1; i < lastIndexOfBy; i++)
					taskName += command.charAt(i);
				for (int i = lastIndexOfBy + KEYWORD_BY.length(); i < command
						.length(); i++)
					byTime += command.charAt(i);

				components = new String[] { taskName,
						KEYWORD_BY.substring(1) + byTime };

			} else if (lastIndexOfFrom > lastIndexOfBy
					&& lastIndexOfFrom > lastIndexOfAt
					&& lastIndexOfFrom < lastIndexOfTo) {

				// start-time-end-time task, format: ... -from ... -to ...

				String taskName = "", fromTime = "", toTime = "";

				for (int i = indexOfFirstSpace + 1; i < lastIndexOfFrom; i++)
					taskName += command.charAt(i);
				for (int i = lastIndexOfFrom + KEYWORD_FROM.length(); i < lastIndexOfTo; i++)
					fromTime += command.charAt(i);
				for (int i = lastIndexOfTo + KEYWORD_TO.length(); i < command
						.length(); i++)
					toTime += command.charAt(i);

				components = new String[] { taskName,
						KEYWORD_FROM.substring(1) + fromTime,
						KEYWORD_TO.substring(1) + toTime };

			} else if (lastIndexOfAs > lastIndexOfAt
					&& lastIndexOfAs > lastIndexOfBy
					&& lastIndexOfAs > lastIndexOfFrom
					&& lastIndexOfAs > lastIndexOfTo) {
				String taskName = "", status = "";
				for (int i = indexOfFirstSpace + 1; i < lastIndexOfAs; i++)
					taskName += command.charAt(i);
				for (int i = lastIndexOfAs + KEYWORD_AS.length(); i < command
						.length(); i++)
					status += command.charAt(i);
				components = new String[] { taskName,
						KEYWORD_AS.substring(1) + status };
			} else {

				// only contain taskname

				components = new String[] { command
						.substring(indexOfFirstSpace + 1) };
			}
		}
		String lastComponent = components[components.length - 1];
		if (lastComponent.endsWith("\n"))
			lastComponent = lastComponent.substring(0,
					lastComponent.length() - 1);
		components[components.length - 1] = lastComponent;
		return components;
	}
	/*
	 * public static void main(String[] args) { String command =
	 * "add go study at school -from 1200 -to 1300"; String[] components =
	 * breakDownCommandIntoComponents(command); for (int i = 0; i <
	 * components.length; i++) System.out.println(components[i]); }
	 */
}
